package com.huitone.smspfm.socket.core.net;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.huitone.smspfm.socket.core.handler.AbstractMessageHandler;
import com.huitone.smspfm.socket.protocol.sgip.constant.CommandID;
import com.huitone.smspfm.socket.protocol.sgip.constant.ErrorCode;
import com.huitone.smspfm.socket.protocol.sgip.message.Message;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.ActiveTest;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.ActiveTestResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.BindResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Deliver;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.DeliverResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.IBody;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Report;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Report2;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Report2Resp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.ReportResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Submit;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Submit2;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Submit2Resp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.SubmitResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.UnBind;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.UnBindResp;
import com.huitone.smspfm.socket.protocol.sgip.message.msghead.Head;

public class DefaultMessageDispacher implements MessageDispacher {
	
	private static final Logger logger = LogManager.getLogger(DefaultMessageDispacher.class);
	
	@Override
	public void dispach(Message message, AbstractMessageHandler handler, ExtSocket socket) {
		
		if (message == null) {
			logger.warn("Message is null");
			return;
		}
		
		if (handler == null) {
			logger.warn("MessageHandler is null");
			return;
		}
		
		Head head = (Head)message.getHead();
		IBody body = message.getBody();
		
		if (head == null) {
			logger.warn("Message Head is null");
			return;
		}
		
		if (body == null) {
			logger.warn("Message body is null");
			return;
		}
		
		switch (CommandID.getCommandIDem(head.getCommandID())) {
		
		case SGIP_BIND:
			
			// 已绑定
			socket.response(message, ErrorCode.CODE_14);
			
			break;
			
		case SGIP_BIND_RESP:
			
			handler.handleBindRespMessage(head, (BindResp)body, socket);
			
			break;
			
		case SGIP_SUBMIT:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleSubmitMessage(head, (Submit)body, socket);
			} else {
				handler.handleSubmitMessage(head, (Submit)body, socket);
			}*/
			handler.handleSubmitMessage(head, (Submit)body, socket);
			
			break;
								
		case SGIP_SUBMIT_RESP:
			
			handler.handleSubmitRespMessage(head, (SubmitResp)body, socket);
			
			break;
			
		case SGIP_DELIVER:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleDeliverMessage(head, (Deliver)body, socket);
			} else {
				handler.handleDeliverMessage(head, (Deliver)body, socket);
			}*/
			handler.handleDeliverMessage(head, (Deliver)body, socket);
			
			break;
			
		case SGIP_DELIVER_RESP:
			
			handler.handleDeliverRespMessage(head, (DeliverResp)body, socket);
			
			break;
			
		case SGIP_REPORT:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleReportMessage(head, (Report)body, socket);
			} else {
				handler.handleReportMessage(head, (Report)body, socket);
			}*/
			
			handler.handleReportMessage(head, (Report)body, socket);
			
			break;
			
		case SGIP_REPORT_RESP:
			
			handler.handleReportRespMessage(head, (ReportResp)body, socket);
			
			break;
			
		case SGIP_UNBIND:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleUnBindMessage(head, (UnBind)body, socket);
			} else {
				handler.handleUnBindMessage(head, (UnBind)body, socket);
			}*/
			
			handler.handleUnBindMessage(head, (UnBind)body, socket);
			
			break;
			
		case SGIP_UNBIND_RESP:
			
			handler.handleUnBindRespMessage(head, (UnBindResp)body, socket);
			
			break;
			
		case SGIP_ACTIVE_TEST:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleActiveTestMessage(head, (ActiveTest)body, socket);
			} else {
				handler.handleActiveTestMessage(head, (ActiveTest)body, socket);
			}*/
			
			handler.handleActiveTestMessage(head, (ActiveTest)body, socket);
			
			break;
			
		case SGIP_ACTIVE_TEST_RESP:
	
			handler.handleActiveTestRespMessage(head, (ActiveTestResp)body, socket);
	
			break;
		case SGIP_SUBMIT2:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleSubmit2Message(head, (Submit2)body, socket);
			} else {
				handler.handleSubmit2Message(head, (Submit2)body, socket);
			}*/
			
			handler.handleSubmit2Message(head, (Submit2)body, socket);
			
			break;
		case SGIP_SUBMIT2_RESP:
			
			handler.handleSubmit2RespMessage(head, (Submit2Resp)body, socket);
	
			break;
			
		case SGIP_REPORT2:
			
			/*if (handler.isAutoResponse()) {
				handler.getProxyHandler().handleReport2Message(head, (Report2)body, socket);
			} else {
				handler.handleReport2Message(head, (Report2)body, socket);
			}*/
			handler.handleReport2Message(head, (Report2)body, socket);
			
			break;
		case SGIP_REPORT2_RESP:
			
			handler.handleReport2RespMessage(head, (Report2Resp)body, socket);
			
			break;
	
		default:
			logger.error("命令不存在:" + message.toString());
			break;
		}
	}
	
	/**
	 * @param msgHead 接收的消息头
	 * @param clazz 响应信息的body类
	 * @param commandID 命令ID
	 * @param socketHolder 通道连接
	 * @return true 通道达到了最大流量限制, false 通道流量还没有达到最大流量限制
	 */
	/*public synchronized void fluxControll(Message message, Class<? extends Resp> clazz, long commandID, ExtSocket socket) {
		
		AccountInfo accountInfo = socket.getAccountInfo();
		
		if (accountInfo.isControlFlux() && accountInfo.isLogin()) {  // 流量控制
			
			if (lastTime == 0) {
				lastTime = socket.getLastReadTimeMillis();
			}
			
			if (!isMaxFlux) {
				curFlux ++;
			}
			
			long interval = socket.getLastReadTimeMillis() - lastTime;
			
			if (interval < 1000) {
				if (curFlux >= accountInfo.getMaxFlux()) {
					logger.info("业务端" + accountInfo.getSrvNode() + "当前流量:" + curFlux + "(条/秒), 最大流量:" + accountInfo.getMaxFlux() + "(条/秒)");
					lastFlux = curFlux;
					isMaxFlux = true;
					socket.response(message, ErrorCode.CODE7);
				}
			} else {
				logger.info("业务端" + accountInfo.getSrvNode() + "当前流量:" + curFlux + "(条/秒), 最大流量:" + accountInfo.getMaxFlux() + "(条/秒)");
				lastFlux = curFlux;
				curFlux = 0;
				lastTime = 0;
				isMaxFlux = false;
			}
		}
	}*/
}
